﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminChatOnlineController : Controller
    {
        //
        // GET: /AdminCP/AdminChatOnline/
        #region "Index"
        public ActionResult Index()
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityChatOnlines
                            select new AdminChatOnlineModel
                            {
                                Id = p.Id,
                                Name = p.Name,
                                Yahoo = p.Yahoo,
                                Skype = p.Sky,
                                Position = p.Position
                            };
                if (query != null)
                    return View(query.ToList());
                else
                    return View("Error");
            }

        }
        #endregion

        #region "Edit"
        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityChatOnlines
                            where p.Id == id
                            select new AdminChatOnlineModel
                            {
                                Id = p.Id,
                                Name = p.Name,
                                Yahoo = p.Yahoo,
                                Skype = p.Sky,
                                Position = p.Position
                            };
                if (query != null)
                    return View(query.FirstOrDefault());

                else
                    return View("Error");
            }

        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AdminChatOnlineModel model)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityChatOnline ChatOnline = db.EntityChatOnlines.FirstOrDefault(p => p.Id == model.Id);
                if (ChatOnline != null)
                {
                    ChatOnline.Name = model.Name;
                    ChatOnline.Yahoo = model.Yahoo;
                    ChatOnline.Sky = model.Skype;
                    ChatOnline.Position = model.Position;
                    db.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                    return View("Error");
            }
        }
        #endregion
    }
}
